/**
 * *****************************************************************
 * @file    spi_acquisition.c
 * @author  Home
 * @version 1.0.0
 * @date    2023-2-10
 * @brief   
 *                 Copyright (c) 2020, Andar Technologies Inc.
 *                           www.andartechs.com 
 *
 * *****************************************************************
 */
/*------------------------------ include -------------------------------------*/
#include "spi_acquisition.h"
#include "adt3102_type_define.h"
#include "gpio_ctype_map.h"
#include "spi_ctype_map.h"
#include "adt3102_spi.h"
#include "adt3102_gpio.h"
#include "adt3102.h"

/*------------------------------ function ------------------------------------*/
/**
 * @brief   dataAcquisitonSpiInit.
 * @param   SpiX: SPI0.
 * @param   spiMode: .
 * @return  None.
 * @note
 */
void dataAcquisitonSpiInit(SPI_TypeDef *SpiX, uint8 spiMode)
{
    if (SpiX == SPI0)
    {
        GPIO->gpio_p03_config=1<<(0+spiMode)|1<<9|1<<10; //sclk  GPIO3
        GPIO->gpio_p04_config=1<<(0+spiMode)|1<<9|1<<10; //csn   GPIO4
        GPIO->gpio_p05_config=1<<(0+spiMode)|1<<9|1<<10; //miso  GPIO5
        GPIO->gpio_p06_config=1<<(0+spiMode)|1<<9|1<<10; //mosi  GPIO6
    }
    
    SpiX->SSPCR0=0x07|0<<6|0<<7;
    SpiX->SSPCR1=0x04;
    SpiX->SSPCPSR=4;
    SpiX->SSPCR1=1<<1|spiMode<<2;
    SpiX->SSPIMSC=1<<1;
}

/**
 * @brief   spi Data Acquisiton.
 * @param   SpiX: SPI0.
 * @param   pbuf: wirte data buf.
 * @param   len:  data length.
 * @param   chirpNum:  number of chirp.
 * @return  None.
 * @note
 */
void spiDataAcquisiton(uint16 type, uint8 *pbuf, uint32 len, uint32 chirpNum)
{
    volatile uint8 temp;
    uint8 cksum = 0;
    uint16 j = 0;
    
    /* send type and chirp number */
    while((SPI0->SSPSR&0x02) != 0x02);
    SPI0->SSPDR = (type>>8);
    while((SPI0->SSPSR&0x04) != 0x04);

    while((SPI0->SSPSR&0x02) != 0x02);
    SPI0->SSPDR = (type & 0x00FF);
    while((SPI0->SSPSR&0x04) != 0x04);
    
    while((SPI0->SSPSR&0x02) != 0x02);
    SPI0->SSPDR = chirpNum;
    while((SPI0->SSPSR&0x04) != 0x04);
    
    /* send data */
    for(int32 i = 0; i < len-8; i = i + 8)
    {
        while((SPI0->SSPSR&0x01) != 0x01);
        SPI0->SSPDR = pbuf[i];
        SPI0->SSPDR = pbuf[i+1];
        SPI0->SSPDR = pbuf[i+2];
        SPI0->SSPDR = pbuf[i+3];
        SPI0->SSPDR = pbuf[i+4];
        SPI0->SSPDR = pbuf[i+5];
        SPI0->SSPDR = pbuf[i+6];
        SPI0->SSPDR = pbuf[i+7];
        cksum = cksum ^ pbuf[i] ^ pbuf[i+1] ^ pbuf[i+2] ^ pbuf[i+3] ^ \
                 pbuf[i+4] ^ pbuf[i+5] ^ pbuf[i+6] ^ pbuf[i+7];
        while((SPI0->SSPSR&0x08) != 0x08);
        
        j = j + 8;
    }
    
    for(int32 k = j; k < len; k++)
    {
        cksum = cksum ^ pbuf[k];
        while((SPI0->SSPSR&0x02) != 0x02);
        SPI0->SSPDR = pbuf[k];
        while((SPI0->SSPSR&0x04) != 0x04);
    }

    /* send check bit */
    while((SPI0->SSPSR&0x02) != 0x02);
    SPI0->SSPDR = ~cksum;
    while((SPI0->SSPSR&0x04) != 0x04);

}

/**
 * @brief   Send data single.
 * @param   dspMemoryStgrTypedef.
 *          See the @dspMemoryStgrTypedef member for details.
 * @return  None.
 * @note
 */
void SendDataSingle(dspMemoryStgrTypedef *memory, uint32 chirpNum)
{
    #if TXRXTYPE == TX1RX1
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0),   \
                memory->samplePoint * 4, chirpNum);
    #elif TXRXTYPE == TX1RX2
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0),   \
                memory->samplePoint * 4, chirpNum);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1),   \
                memory->samplePoint * 4, chirpNum);        
    #elif TXRXTYPE == TX2RX2
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0),   \
                memory->samplePoint * 4, chirpNum);
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0 + memory->samplePoint * 4),   \
                memory->samplePoint * 4, chirpNum);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1),   \
                memory->samplePoint * 4, chirpNum);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1 + memory->samplePoint * 4),   \
                memory->samplePoint * 4, chirpNum);
    #endif
}

/**
 * @brief   Send data single.
 * @param   dspMemoryStgrTypedef.
 *          See the @dspMemoryStgrTypedef member for details.
 * @return  None.
 * @note
 */
void SendDataCont(dspMemoryStgrTypedef *memory)
{
#if TXRXTYPE == TX1RX1
    for(uint32 i = 0; i < memory->chirpNum; i++)
    {
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0 + memory->samplePoint * i * 4),   \
            memory->samplePoint * 4 , i);
    }
#elif TXRXTYPE == TX1RX2
    for(uint32 i = 0; i < memory->chirpNum; i++)
    {
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0 + memory->samplePoint * i * 4),   \
            memory->samplePoint * 4 , i);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1 + memory->samplePoint * i * 4),   \
            memory->samplePoint * 4 , i);
    }       
#elif TXRXTYPE == TX2RX2
    for(uint32 i = 0; i < memory->chirpNum * 2; i = i + 2)
    {
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0 + memory->samplePoint * i * 4),   \
                memory->samplePoint * 4, i/2);
        spiDataAcquisiton(0x55AA, (uint8*)(memory->srcADCR0 + memory->samplePoint * (i + 1) * 4),   \
                memory->samplePoint * 4, i/2);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1 + memory->samplePoint * i * 4),   \
                memory->samplePoint * 4, i/2);
        spiDataAcquisiton(0xAA55, (uint8*)(memory->srcADCR1 + memory->samplePoint * (i + 1) * 4),   \
                memory->samplePoint * 4, i/2);
    }
#endif
}
